<?php
function md5crypt($password){
    // create a salt that ensures crypt creates an md5 hash
    $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                    .'abcdefghijklmnopqrstuvwxyz0123456789+/';
    $salt='$1$';
    /* for($i=0; $i<9; $i++){
        $salt.=$base64_alphabet[rand(0,63)];
    } */
    // return the crypt md5 password
	$iterations = 2;
    $password = md5($password);
	for ($i = 0; $i < $iterations; ++$i)
	{
		$password = md5($password);
	}
    //return md5($password,$salt.'$');
	return md5($password);
}

//La base de l'application'
$base_url = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '')
                      .'://'.$_SERVER['HTTP_HOST'].str_replace('//','/',dirname($_SERVER['SCRIPT_NAME']).'/');


$conflen=strlen('functions');
$B=substr(__FILE__,0,strrpos(__FILE__,'/'));
$A=substr($_SERVER['DOCUMENT_ROOT'], strrpos($_SERVER['DOCUMENT_ROOT'], $_SERVER['PHP_SELF']));
$C=substr($B,strlen($A));
$posconf=strlen($C)-$conflen-1;
$D=substr($C,1,$posconf);
$host='http:'.((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '').'//'.$_SERVER['SERVER_NAME'].'/'.$D;
$app_folder = $host.'gapb/';

$il_ya_7jours  = mktime(0, 0, 0, date("m")  , date("d")-7, date("Y"));
$demain  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$aujourdhui  = mktime(date("H"), date("i"), date("s"), date("m")  , date("d"), date("Y"));
$nbre_jr_mois = date( "t", mktime ( 0, 0, 0, date("m"), 1, date("Y") ) );
$premier_jr_mois = mktime ( 0, 0, 0, date("m"), 1, date("Y") );
$dernier_jr_mois = mktime ( 23, 59, 59, date("m"), $nbre_jr_mois, date("Y") );
$date_debut_mois = date('d M Y', $premier_jr_mois);
$date_fin_mois = date('d M Y', $dernier_jr_mois);

// Avoir l'url actuel de la page
function getUrl() {
	$url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
	$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
	$url .= $_SERVER["REQUEST_URI"];
	return $url;
}

function form_error($field){
    return TRUE;
}

/*
* Fonction permettant de fabriquer une requete SQL
*   mysql_insert('cars', array(
*    'make' => 'Aston Martin',
*    'model' => 'DB9',
*    'year' => '2009',
*   ));
*/
function mysql_insert($table, $inserts) {
    $values = array_map('mysql_real_escape_string', array_values($inserts));
    $keys = array_keys($inserts);     
    return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}


function forceRedirect($url = '/'){
    if(!headers_sent()) {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location:'.$url);  
        header('Connection: close');
        exit;
    }
    else {
        echo 'location.replace('.$url.');';
    }
    exit;
}

if(!isset($_SESSION['userid']))
{
    $_SESSION['next'] = getUrl();
    //forceRedirect('../sessions/login.php');
}
?> 